package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import cn.wh.xwm.controller.action.vo.DifferentView;
import cn.wh.xwm.controller.action.vo.SelectView;
import cn.wh.xwm.controller.action.vo.report.custinout.CustomerInAndOutJson;
import cn.wh.xwm.controller.action.vo.report.sales.SalesJson;
import cn.wh.xwm.hibernate.dao.Customerinfo;
import cn.wh.xwm.hibernate.dao.CustomerinfoDAO;
import cn.wh.xwm.hibernate.dao.PmLiquidDAO;
import cn.wh.xwm.hibernate.dao.ProductinfoDAO;
import cn.wh.xwm.hibernate.dao.SalesLiquid;
import cn.wh.xwm.hibernate.dao.SalesLiquidDAO;
import cn.wh.xwm.hibernate.dao.UserCustomerRelation;
import cn.wh.xwm.hibernate.dao.UserCustomerRelationDAO;

/**
 * 报表管理 ACTION
 * @author swimming
 *
 */
public class ReportAction extends BaseAction {
	
	private String startDate;
	private String endDate;
	private String customerLevelId;
	private String customerNameId;
	private String eventType;//0采购入库，1销售出库
	private String productId;
	
	private String salesId;
	
	public String initCustomerInandOut(){
		return "initCustomerInandOut";
	}
	public String initSalesLiquid(){
		return "initSalesLiquid";
	}
	public String initdifferent(){
		return "initdifferent";
	}
	public String initinventoryadjust(){
		return "initinventoryadjust";
	}
	
	/**
	 * 客户入库出库报表
	 * @throws IOException
	 * @throws ParseException 
	 */
	public void findCustomerInandOut() throws IOException, ParseException{
		PmLiquidDAO dao = new PmLiquidDAO();
		CustomerInAndOutJson json =  dao.findForReport(customerNameId,eventType,startDate,endDate);
		JSONObject object = JSONObject.fromObject(json);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 查询sales销售报表
	 * @throws IOException
	 */
	public void findSalesreport() throws IOException{
		SalesLiquidDAO dao = new SalesLiquidDAO();
		SalesJson json =  dao.findForReport(customerNameId,salesId,startDate,endDate);
		JSONObject object = JSONObject.fromObject(json);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 出入对比报表
	 * @throws IOException
	 */
	public void findDifferentReport() throws IOException{
		PmLiquidDAO dao = new PmLiquidDAO();
		CustomerInAndOutJson json =  dao.findForReport(salesId,startDate,endDate);
		JSONObject object = JSONObject.fromObject(json);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 查询sales详情
	 * @throws IOException 
	 */
	public void findSalesDetails() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		ProductinfoDAO pdao = new ProductinfoDAO();
		PmLiquidDAO pmdao = new PmLiquidDAO();
		UserCustomerRelationDAO userCustomerRelationDAO =  new UserCustomerRelationDAO();
		List<UserCustomerRelation> list = userCustomerRelationDAO.findByProperty("id.userId", salesId);
		ArrayList<DifferentView> vlist = new ArrayList<DifferentView>();
		for(UserCustomerRelation info :list){
			DifferentView view = new DifferentView();
			view.setCustomerName(dao.findById(info.getId().getCustomerId()).getName());
			view.setProductName(pdao.findById(productId).getName());
			view.setAmount(pmdao.findbySales(salesId, productId, info.getId().getCustomerId(), startDate, endDate));
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", list.size());
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 查询PM详情
	 * @throws IOException 
	 */
	public void findPmDetails() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		ProductinfoDAO pdao = new ProductinfoDAO();
		PmLiquidDAO pmdao = new PmLiquidDAO();
		UserCustomerRelationDAO userCustomerRelationDAO =  new UserCustomerRelationDAO();
		List<UserCustomerRelation> list = userCustomerRelationDAO.findByProperty("id.userId", salesId);
		ArrayList<DifferentView> vlist = new ArrayList<DifferentView>();
		for(UserCustomerRelation info :list){
			DifferentView view = new DifferentView();
			view.setCustomerName(dao.findById(info.getId().getCustomerId()).getName());
			view.setProductName(pdao.findById(productId).getName());
			view.setAmount(pmdao.findbyPM(salesId, productId, info.getId().getCustomerId(), startDate, endDate));
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", list.size());
		dao.getSession().close();
		this.responseJson(object);
	}
	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getCustomerLevelId() {
		return customerLevelId;
	}

	public void setCustomerLevelId(String customerLevelId) {
		this.customerLevelId = customerLevelId;
	}

	public String getCustomerNameId() {
		return customerNameId;
	}

	public void setCustomerNameId(String customerNameId) {
		this.customerNameId = customerNameId;
	}

	public String getEventType() {
		return eventType;
	}

	public void setEventType(String eventType) {
		this.eventType = eventType;
	}
	public String getSalesId() {
		return salesId;
	}
	public void setSalesId(String salesId) {
		this.salesId = salesId;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	
	
}
